<?php


namespace app\manage\controller;


use app\lib\exception\ParamException;
use think\Db;

/**
 * 与百度相关的
 */
class Baidu extends Base
{
    public function _initialize()
    {
        parent::_initialize();
    }

    public function flow()
    {
        $this->index(1);
        $this->assign('title','百度信息流');
        $this->assign('type_name','flow');
        return $this->fetch('index');
    }

    public function search()
    {
        $this->index(2);
        $this->assign('title','百度搜索');
        $this->assign('type_name','search');
        return $this->fetch('index');
    }

    public function index($type)
    {
        // 查询条件start
        $admin_id = input('admin_id',0);
        $acc_name = input('acc_name','');
        $campaignId = input('campaignId',0);
        $campaignNameStatus = input('campaignNameStatus','');
        $adGroupNameStatus = input('adGroupNameStatus','');
        $ideaName = input('ideaName','');
        $wInfoNameStatus = input('wInfoNameStatus','');
        $default = date('Y-m-d', strtotime('-7 days')).' - '.date('Y-m-d');
        $times = input('times',$default);
        $report_type = input('report_type',1);

        $map = [];
        if($report_type == 1){
            $map['campaignId'] = 0;
            $map['adGroupId'] = 0;
            $map['ideaId'] = 0;
            $field = 'admin_id';
            $group = 'admin_id';
        }elseif ($report_type == 2){
            $map['campaignId'] = 0;
            $map['adGroupId'] = 0;
            $map['ideaId'] = 0;
            $field = 'admin_id,userName';
            $group = 'userName';
        }elseif ($report_type == 3){
            $map['campaignId'] = ['<>',0];
            $map['adGroupId'] = 0;
            $map['ideaId'] = 0;
            $field = 'admin_id,userName,campaignNameStatus,campaignId';
            $group = 'campaignId';
        }elseif ($report_type == 4){
            $map['adGroupId'] = ['<>',0];
            $map['ideaId'] = 0;
            $field = 'admin_id,userName,campaignNameStatus,adGroupNameStatus,adGroupId';
            $group = 'adGroupId';
        }elseif ($report_type == 5){
            $map['ideaId'] = ['<>',0];
            $field = 'admin_id,userName,campaignNameStatus,adGroupNameStatus,ideaName,ideaId';
            $group = 'ideaId';
        }elseif ($report_type == 6){
            $map['wInfoId'] = ['<>',0];
            $field = 'admin_id,userName,campaignNameStatus,adGroupNameStatus,wInfoNameStatus,wInfoId';
            $group = 'wInfoId';
        }

        $map['type'] = $type;

        if($admin_id != 0){
            $map['admin_id'] = $admin_id;
        }

        if($campaignId != 0){
            $map['campaignId'] = $campaignId;
        }

        if($acc_name != ''){
            $map['userName'] = $acc_name;
        }

        if($campaignNameStatus != ''){
            $map['campaignNameStatus'] = $campaignNameStatus;
        }

        if($adGroupNameStatus != ''){
            $map['adGroupNameStatus'] = $adGroupNameStatus;
        }

        if($ideaName != ''){
            $map['ideaName'] = $ideaName;
        }

        if($wInfoNameStatus != ''){
            $map['wInfoNameStatus'] = $wInfoNameStatus;
        }

        if($times != ''){
            $time_arr = explode(' - ',$times);
            $map['date'] = ['between',$time_arr];
        }
        // 查询条件end


        // 不是开发者或者超级管理员则只能看自己的数据
        $where_admin = $where_account = [];
        $where_account['channel_id'] = 1;
        if($this->uid != 1 && $this->group_id != 1){
            $where_admin['id'] = $this->uid;
            $where_account['admin_id'] = $this->uid;
            $map['admin_id'] = $this->uid;
        }

        $admins = model('Admin')->getListByMapAndOrder($where_admin, '', '',true,'id desc');
        $accounts = model('Account')->getListByMapAndOrder($where_account, '', '',true,'id desc');

        $infos = model('BaiduReport')->where($map)
            ->field($field.',sum(impression) impression_t,sum(click) click_t,sum(cost) cost_t,sum(ctr) ctr_t,sum(cpc) cpc_t,sum(cpm) cpm_t,sum(aggrFormClickSuccess) clickSuccess_t,sum(aggrFormSubmitSuccess) submitSuccess_t')
            ->order('date desc')->group($group)->paginate()->each(function ($item, $key){
                $item['admin_name'] = $this->getAdminName($item['admin_id']);
                if($item['impression_t'] != 0){
                    $item['cost_impression'] = $item['cost_t']/$item['impression_t']*1000;
                    $item['click_impression'] = $item['click_t']/$item['impression_t'];
                }else{
                    $item['cost_impression'] = 0;
                    $item['click_impression'] = 0;
                }
                if($item['click_t'] != 0){
                    $item['cost_click'] = $item['cost_t']/$item['click_t'];
                }else{
                    $item['cost_click'] = 0;
                }
                return $item;
            });

        $this->assign('type',$type);
        $this->assign('report_type',$report_type);
        $this->assign('infos',$infos);
        $this->assign('admins',$admins);
        $this->assign('accounts',$accounts);
        $this->assign('times',$times);
        $this->assign('admin_id',$admin_id);
        $this->assign('acc_name',$acc_name);
        $this->assign('campaignNameStatus',$campaignNameStatus);
        $this->assign('adGroupNameStatus',$adGroupNameStatus);
        $this->assign('ideaName',$ideaName);
        $this->assign('wInfoNameStatus',$wInfoNameStatus);


    }


}